package com.haoniu.web.controller.module;

import com.haoniu.framework.util.ShiroUtils;
import com.haoniu.module.domain.dto.UserInviteDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.haoniu.common.annotation.Log;
import com.haoniu.common.enums.BusinessType;
import com.haoniu.module.domain.database.UserInvite;
import com.haoniu.module.service.UserInviteService;
import com.haoniu.web.core.base.BaseController;
import com.haoniu.framework.web.page.TableDataInfo;
import com.haoniu.common.base.AjaxResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户邀请关系 信息操作处理
 *
 * @author houyijun
 * @date 2019-01-30
 */
@Controller
@RequestMapping("/module/userInvite")
public class UserInviteController extends BaseController
{
    private String prefix = "module/userInvite";

	@Autowired
	private UserInviteService userInviteService;

	@RequiresPermissions("module:userInvite:view")
	@GetMapping()
	public String userInvite()
	{
	    return prefix + "/userInvite";
	}

	/**
	 * 查询用户邀请关系列表
	 */
	@RequiresPermissions("module:userInvite:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list() {
		startPage();
		Map<String, Object> map = new HashMap<>();

        List<UserInviteDTO> list = userInviteService.getUserInviteList(map);
		return getDataTable(list);
	}

	/**
	 * 一级推荐
	 * @return
	 */
	@RequiresPermissions("module:userInvite:oneList")
	@PostMapping("/oneList")
	@ResponseBody
	public TableDataInfo oneList(UserInviteDTO userInviteDTO) {
		startPage();
		List<UserInviteDTO> list = userInviteService.getInviteOneList(userInviteDTO.getUserId());
		return getDataTable(list);
	}


	/**
	 * 新增用户邀请关系
	 */
	@GetMapping("/add")
	public String add() {
	    return prefix + "/add";
	}

	/**
	 * 新增保存用户邀请关系
	 */
	@RequiresPermissions("module:userInvite:add")
	@Log(title = "用户邀请关系", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(UserInvite userInvite) {
        userInvite.setCreateBy(ShiroUtils.getLoginName());
		return toAjax(userInviteService.insertUserInvite(userInvite));
	}

	/**
	 * 修改用户邀请关系
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
		UserInvite userInvite = userInviteService.selectUserInviteById(id);
		mmap.put("userInvite", userInvite);
	    return prefix + "/edit";
	}

	/**
	 * 修改保存用户邀请关系
	 */
	@RequiresPermissions("module:userInvite:edit")
	@Log(title = "用户邀请关系", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(UserInvite userInvite) {
        userInvite.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(userInviteService.updateUserInvite(userInvite));
	}

	/**
	 * 删除用户邀请关系
	 */
	@RequiresPermissions("module:userInvite:remove")
	@Log(title = "用户邀请关系", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(userInviteService.deleteUserInviteByIds(ids));
	}

	/**
	 * 一级推荐
	 */
	@RequiresPermissions("module:userInvite:oneList")
	@GetMapping("/detail/{userId}")
	public String detail(@PathVariable("userId") Integer userId, ModelMap mmap) {
		mmap.put("userId", userId);
		return "module/userInvite/oneInvite";
	}
}
